import {createRouter,createWebHistory} from 'vue-router'
import routes from './routes'
import useGlobalStore from '../store/modules/global'
import { ElMessage } from 'element-plus';
const router = createRouter({
    history:createWebHistory(),
    routes
})
router.beforeEach((to:any,from:any,next:any) =>  {
    const globalStore =useGlobalStore()
    if(to.path == '/login'){
        next()
    }else{
        if(globalStore.adminAuthority){
            if(! globalStore.token ||  globalStore.token == ''){
                ElMessage.warning('您还未登录，请先登录！');
                next('/login')
            }else{
                if(to.path == '/' && from.path == '/login'){
                    ElMessage({
                        message: '登录成功',
                        type: 'success',
                      })
                }
                next()
            }
        }else{
            ElMessage.warning('您没有权限进入本系统！');
            next('/login')
        }
    }   
})

export default router